package com.example.springmvc_demo2.service.impl;

import com.example.springmvc_demo2.entity.User;
import com.example.springmvc_demo2.mapper.UserMapper;
import com.example.springmvc_demo2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
public class UserServiceImpl implements UserService {

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public boolean registerUser(User user) {
        // 检查用户名、手机号或邮箱是否已存在
        if (userMapper.findByUsername(user.getUsername()) != null ||
            (user.getPhone() != null && !user.getPhone().isEmpty() && userMapper.findByPhone(user.getPhone()) != null) ||
            (user.getEmail() != null && !user.getEmail().isEmpty() && userMapper.findByEmail(user.getEmail()) != null)) {
            return false; // 用户已存在
        }
        // 对密码进行加密
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        return userMapper.insertUser(user) > 0;
    }

    @Override
    public User login(String identifier, String password) {
        logger.info("Attempting to log in user with identifier: {}", identifier);
        User user = null;
        // 尝试通过用户名、手机号或邮箱查找用户
        if (identifier.contains("@")) { // 可能是邮箱
            user = userMapper.findByEmail(identifier);
            logger.info("Attempting to find user by email: {}. Found: {}", identifier, user != null);
        } else if (identifier.matches("\\d+")) { // 可能是手机号
            user = userMapper.findByPhone(identifier);
            logger.info("Attempting to find user by phone: {}. Found: {}", identifier, user != null);
        } else { // 假设是用户名
            user = userMapper.findByUsername(identifier);
            logger.info("Attempting to find user by username: {}. Found: {}", identifier, user != null);
        }

        if (user != null) {
            logger.info("User found: {}. Attempting to match password.", user.getUsername());
            if (passwordEncoder.matches(password, user.getPassword())) {
                logger.info("Password matched for user: {}. Login successful.", user.getUsername());
                return user; // 登录成功
            } else {
                logger.warn("Password mismatch for user: {}. Input password: {}, Stored password: {}", user.getUsername(), password, user.getPassword());
            }
        } else {
            logger.warn("User not found for identifier: {}", identifier);
        }
        return null; // 登录失败
    }

    @Override
    public boolean updatePassword(String username, String newPassword) {
        User user = userMapper.findByUsername(username);
        if (user == null) {
            return false; // 用户不存在
        }
        String encodedPassword = passwordEncoder.encode(newPassword);
        return userMapper.updatePasswordByUsername(username, encodedPassword) > 0;
    }
} 